package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.vo.OrderVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.PurchaseOrders;
import cn.springboot.erp.service.PurchaseOrdersService;
import cn.springboot.erp.mapper.PurchaseOrdersMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author asus
* @description 针对表【purchase_orders】的数据库操作Service实现
* @createDate 2024-05-13 09:33:10
*/
@Service
public class PurchaseOrdersServiceImpl extends ServiceImpl<PurchaseOrdersMapper, PurchaseOrders>
    implements PurchaseOrdersService{

    @Override
    public ResponseResult listPage(QueryParams<PurchaseOrders> queryParams) {
        Integer limit = queryParams.getLimit();
        Integer page = queryParams.getPage();
        PurchaseOrders searchParams = queryParams.getSearchParams();
        Page<PurchaseOrders> purchaseOrdersPage = new Page<>(page, limit);
        LambdaQueryWrapper<PurchaseOrders> lambda = new QueryWrapper<PurchaseOrders>().lambda();
        lambda.eq(StringUtils.isNotBlank(searchParams.getSupplierName()),PurchaseOrders::getSupplierName,searchParams.getSupplierName())
                .eq(StringUtils.isNotBlank(searchParams.getName()),PurchaseOrders::getName,searchParams.getName())
                .orderByDesc(PurchaseOrders::getCreateTime);
        baseMapper.selectPage(purchaseOrdersPage,lambda);

        return ResponseResult.Success()
                .data("total",purchaseOrdersPage.getTotal())
                .data("list",purchaseOrdersPage.getRecords());
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        PurchaseOrders one = baseMapper.selectById(id);
        if (one.getStatus()==0){
            return ResponseResult.Fail().msg("采购未完成，暂时不能删除");
        }
        baseMapper.deleteById(id);
        return ResponseResult.Success().msg("删除成功");
    }
}




